﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace r.libr
{
    public class Treetable
    {
        private Hashtable TreeHash = new Hashtable();

        public void AddNode(string path)
        {
            AddLeaf(path, new Hashtable());
        }
        public void AddLeaf(string path, object Value)
        {
            object[] convPath = ConvertPath(path);
            ((Hashtable)convPath[0]).Add(convPath[1], Value);
        }
        public Hashtable GetNode(string path)
        {
            return ((Hashtable)GetLeaf(path));
        }
        public object GetLeaf(string path)
        {
            object[] convPath = ConvertPath(path);
            return (((Hashtable)convPath[0])[convPath[1]]);
        }
        public void SetLeaf(string path, object Value)
        {
            object[] convPath = ConvertPath(path);
            ((Hashtable)convPath[0])[convPath[1]] = Value;
        }
        private object[] ConvertPath(string path)
        {
            Hashtable tempTreeHash = TreeHash;
            string[] splitPath = path.Split(new Char[] { '.' });
            for (int i = 0; i < splitPath.Length - 1; i++)
            {
                tempTreeHash = (Hashtable)tempTreeHash[splitPath[i]];
            }
            return (new object[] { tempTreeHash, splitPath[splitPath.Length - 1] });
        }

    }
}
